import 'package:flutter/material.dart';

class AnimationDemo extends StatelessWidget {
  const AnimationDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('AnimationDemo'),
        elevation: 0.0,
      ),
      body: AnimationDemoHome(),
    );
  }
}

class AnimationDemoHome extends StatefulWidget {
  const AnimationDemoHome({super.key});

  @override
  State<AnimationDemoHome> createState() => _AnimationDemoHomeState();
}

class _AnimationDemoHomeState extends State<AnimationDemoHome>
    with TickerProviderStateMixin {
  late AnimationController animationController;
  late Animation animation;
  late Animation animationColor;
  late CurvedAnimation curve;


  @override
  void initState() {
    super.initState();
    
    animationController = AnimationController(
        // value: 32.0,
        // lowerBound: 32.0,
        // upperBound: 100.0,
        duration: const Duration(milliseconds: 3000),
        vsync: this);

    curve = CurvedAnimation(parent: animationController, curve: Curves.bounceInOut);

    animation = Tween(begin: 32.0, end: 100.0).animate(curve);
    animationColor = ColorTween(begin: Colors.red, end: Colors.red[900]).animate(curve);

    animationController.addListener(() {
      // print('${animationController.value}');
      setState(() {});
    });

    animationController.addStatusListener((status) {
      print(status);
    });

    // animationController.forward(); //动画执行
  }

  @override
  void dispose() {
    super.dispose();
    animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimatedHeart(
        animations: [
          animation,
          animationColor
        ],
        controller: animationController,
      )
    );
        // child: IconButton(
        //     icon: Icon(Icons.favorite),
        //     // iconSize: animationController.value,
        //     iconSize: animation.value,
        //     color: animationColor.value,
        //     onPressed: () {
        //       switch(animationController.status){
        //         case AnimationStatus.completed:
        //           animationController.reverse();
        //         break;
        //         default:
        //           animationController.forward();
        //         break;
        //       }
        //     }));
  }
}

class AnimatedHeart extends  AnimatedWidget{
  final List animations;
  final AnimationController controller;

  AnimatedHeart({
    required this.animations,
    required this.controller
  }): super(listenable: controller);

  @override
  Widget build(BuildContext context) {
    return Center(
        child: IconButton(
            icon: Icon(Icons.favorite),
            iconSize: animations[0].value,
            color: animations[1].value,
            onPressed: () {
              switch(controller.status){
                case AnimationStatus.completed:
                  controller.reverse(); //动画恢复
                break;
                default:
                  controller.forward();//动画执行
                break;
              }
            }));
  }
}